<?php

/**
 * @Author: Wang Chunsheng 2192138785@qq.com
 * @Date:   2020-03-02 21:39:11
 * @Last Modified by:   Wang Chunsheng 2192138785@qq.com
 * @Last Modified time: 2020-03-29 03:20:33
 */


namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\web\HttpException;
use diandi\admin\components\Helper;
use diandi\admin\models\Menu;
use diandi\adminlte\AdminLteAsset;
use diandi\addons\modules\searchs\DdAddons;


class BaseController extends Controller
{
    public $layout = "@backend/views/layouts/main";

    public function beforeAction($action)
    {
        //没有登录则跳转到登录界面
        if (Yii::$app->user->isGuest && \Yii::$app->controller->id != 'site') {
            return  $this->redirect(Yii::$app->urlManager->createUrl(Yii::$app->user->loginUrl));
        }
        $requestedRoute = "/" . \Yii::$app->controller->id . "/" . \Yii::$app->controller->action->id;
        $url = \Yii::$app->controller->module->id . $requestedRoute;

        $DdAddons = new DdAddons();
        $modules = $DdAddons::find()->select('identifie')->asArray()->column();
        if (in_array(Yii::$app->controller->module->id, $modules)) {
            $this->layout = "@backend/views/layouts/plugins-base";
            Yii::$app->params['plugins'] =  Yii::$app->controller->module->id;
        }
        $requestedRoute = "/" . \Yii::$app->controller->id;
        $nav  = Yii::$app->service->backendNavService->getMenu();
        // $menutypes = Menu::find()->where(['like', 'route', $requestedRoute])->select(['type'])->one();
        /*初始化当前菜单类别*/
        Yii::$app->params['plugins'] = $nav['top'][0]['mark']; //  empty($menutypes['type']) ? $nav['top'][0]['mark'] : $menutypes['type'];
        // 初始化菜单
        Yii::$app->params['topNav'] = Yii::$app->service->backendNavService->getMenu('top');
        Yii::$app->params['leftNav'] = Yii::$app->service->backendNavService->getMenu('left');
        /* 设置模板主题 */
        Yii::$app->params['Website']['themcolor'] = Yii::$app->settings->get('Website', 'themcolor');

        return parent::beforeAction($action);
    }

    /**
     * {@inheritdoc}
     */
    public function actions()
    {

        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }
}
